package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.nursing.domain.pojo.DeviceData;
import com.zzyl.nursing.domain.pojo.Room;
import com.zzyl.nursing.domain.vo.BedVo;
import com.zzyl.nursing.domain.vo.DeviceInfo;
import com.zzyl.nursing.domain.vo.RoomVo;
import com.zzyl.nursing.mapper.RoomMapper;
import com.zzyl.nursing.service.IRoomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 房间Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-26
 */
@Service
@Slf4j
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements IRoomService {
    @Autowired
    private RoomMapper roomMapper;

    /**
     * 查询房间
     *
     * @param id 房间主键
     * @return 房间
     */
    @Override
    public Room selectRoomById(Long id) {
        return getById(id);
    }

    /**
     * 查询房间列表
     *
     * @param room 房间
     * @return 房间
     */
    @Override
    public List<Room> selectRoomList(Room room) {
        return roomMapper.selectRoomList(room);
    }

    /**
     * 新增房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int insertRoom(Room room) {
        return save(room) ? 1 : 0;
    }

    /**
     * 修改房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int updateRoom(Room room) {
        return updateById(room) ? 1 : 0;
    }

    /**
     * 批量删除房间
     *
     * @param ids 需要删除的房间主键
     * @return 结果
     */
    @Override
    public int deleteRoomByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 根据楼层 id 获取房间视图对象列表
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsByFloorId(Long floorId) {
        return roomMapper.selectByFloorId(floorId);
    }


    /**
     * 获取所有房间（负责老人）
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsWithNurByFloorId(Long floorId) {
        return roomMapper.selectByFloorIdWithNur(floorId);
    }

    @Override
    public RoomVo getRoom(Long id) {
        System.out.println("id = " + id);
      return roomMapper.getRoom(id);

    }

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public List<RoomVo> getRoomsWithDeviceByFloorId(Long floorId) {
        List<RoomVo> roomVos =  roomMapper.getRoomsWithDeviceByFloorId(floorId);

        roomVos.forEach(roomVo ->
                {
                    List<DeviceInfo> deviceVos = roomVo.getDeviceVos();
                    deviceVos.forEach(deviceInfo -> {
                        String jsonStr = (String) stringRedisTemplate.opsForHash().get(CacheConstants.IOT_DEVICE_LAST_DATA, deviceInfo.getIotId());
                        if(StrUtil.isBlank(jsonStr)){
                            return;
                        }
                        deviceInfo.setDeviceDataVos(JSONUtil.toList(jsonStr, DeviceData.class));
                    });
                    List<BedVo> bedVoList = roomVo.getBedVoList();
                    bedVoList.forEach(bedVo -> {
                        bedVo.getDeviceVos().forEach(deviceInfo -> {
                            String jsonStr = (String) stringRedisTemplate.opsForHash().get(CacheConstants.IOT_DEVICE_LAST_DATA, deviceInfo.getIotId());
                            if(StrUtil.isBlank(jsonStr)){
                                return;
                            }
                            deviceInfo.setDeviceDataVos(JSONUtil.toList(jsonStr, DeviceData.class));
                        });
                    });
                }
                );
//        if (CollUtil.isEmpty(roomVos)){
//            return null;
//        }
//
//        Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries(CacheConstants.IOT_DEVICE_LAST_DATA);
//        if (CollUtil.isEmpty(entries)) {
//            System.out.println("entries = " + entries);
//            return roomVos;
//        }
//        Map<String, List<DeviceData>> deviceDataMap = entries.entrySet().stream().collect(Collectors.toMap(entry -> entry.getValue().toString(), entry -> JSONUtil.toList(entry.getValue().toString(), DeviceData.class)));
//
//        roomVos.forEach(roomVo -> {
//            //填充数据
//            List<DeviceInfo> deviceVosRoom = roomVo.getDeviceVos();
//            if (CollUtil.isNotEmpty(deviceVosRoom)) {
//                deviceVosRoom.forEach(deviceInfo -> {
//                    System.out.println(" ====================== ");
//                    deviceInfo.setDeviceDataVos(deviceDataMap.getOrDefault(deviceInfo.getIotId(),List.of()));
//                });
//                List<BedVo> bedVoList = roomVo.getBedVoList();
//                if (CollUtil.isNotEmpty(bedVoList)) {
//                    bedVoList.forEach(bedVo -> {
//                        List<DeviceInfo> deviceVos = bedVo.getDeviceVos();
//                        if(CollUtil.isNotEmpty(deviceVos)){
//                            deviceVos.forEach(deviceInfo -> {
//                                System.out.println(" ====================== ");
//                                deviceInfo.setDeviceDataVos(deviceDataMap.getOrDefault(deviceInfo.getIotId(),List.of()));
//                            });
//                        }
//                    });
//                }
//            }
//        });
        return roomVos;
    }
}
